#import "OmPersonSignVC.h"
@interface OmPersonSignVC ()
@property (weak, nonatomic) IBOutlet UITextView *textView;
@end
@implementation OmPersonSignVC
- (void)viewDidLoad {
    [super viewDidLoad];
    ViewBorderRadius(self.textView, 0, 1, HexadecimalColor(@"#e5e5e5"));
    self.navigationItem.title = ASLocalizedString(@"座右铭");
    self.textView.textContainerInset = UIEdgeInsetsMake(12, 12, 0, 0);
    [self setHiddenForTabBar:YES navigationBar:NO];
    [self setPlaceholder:ASLocalizedString(@"请输入座右铭") color:HexadecimalColor(@"#999999")];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
}
- (void)setPlaceholder:(NSString *)placeholder color:(UIColor * _Nullable )color {
    UILabel *placeholderLbl = [self.textView valueForKey:@"_placeholderLabel"];
    if (!placeholderLbl) {
        placeholderLbl = [[UILabel alloc] init];
        placeholderLbl.numberOfLines = 0;
        placeholderLbl.font = self.textView.font;
        [placeholderLbl sizeToFit];
        placeholderLbl.textColor = [UIColor colorWithRed:214 / 255.0 green:214 / 255.0 blue:214 / 255.0 alpha:1.0];
        [self.textView addSubview:placeholderLbl];
        [self.textView setValue:placeholderLbl forKey:@"_placeholderLabel"];
    }
    if (color) {
        placeholderLbl.textColor = color;
    }
    placeholderLbl.text = placeholder;
}
- (IBAction)completeInput:(id)sender {
    NSString *value = self.textView.text;
    value = [value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if (!StrValid(value)) {
        [MBProgressHUD showErrorMessage:ASLocalizedString(@"请先输入非空格内容")];
        return;
    }
    [self.textView resignFirstResponder];
    [self loadRequest:value];
}
#pragma mark - 网络加载
- (void)loadRequest:(NSString *)string {
    [[HUDHelper sharedInstance] loading:ASLocalizedString(@"更新中...")];
    NSString *url = [[CYURLUtils sharedCYURLUtils] makeURLWithC:@"user_api" A:@"save_user_field"];
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"key_name"] = @"sign";
    parameters[@"key_value"] = string;
    [CYNET POST:url parameters:parameters responseCache:nil success:^(id responseObject) {
        [MBProgressHUD hideHUD];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [MBProgressHUD showSuccessMessage:ASLocalizedString(@"座右铭修改成功")];
            [self changeSuccess];
        });
    } failure:^(NSString *error) {
        [MBProgressHUD hideHUD];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [MBProgressHUD showTipMessageInView:error];
        });
    } hasCache:NO];
}
- (void)changeSuccess {
    NSString *value = self.textView.text;
    value = [value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    curUser.signature = value;
    [userManager saveUserInfo];
    [self.navigationController popViewControllerAnimated:YES];
}
@end
